package com.yiyuzi.admin.user;

import com.yiyuzi.admin.common.constant.ResponseCodeEnum;
import com.yiyuzi.admin.common.domain.IDQueryDTO;
import com.yiyuzi.admin.common.domain.PageResultDTO;
import com.yiyuzi.admin.common.domain.ResponseDTO;
import com.yiyuzi.admin.common.utils.ConvertUtils;
import com.yiyuzi.admin.user.domain.dto.*;
import com.yiyuzi.admin.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author yiyuzi
 * @date 2021/5/6 22:16
 */

@RestController
@RequestMapping("user")
@Api(tags = "用户")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登入")
    public ResponseDTO<String> login(UserLoginDTO userLoginDTO){
        return ResponseDTO.res(userService.login(userLoginDTO), ResponseCodeEnum.LOGIN_FAIL);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "登出")
    public ResponseDTO<String> logout(HttpServletRequest request){
        return ResponseDTO.res(userService.logout(request.getHeader("token")), ResponseCodeEnum.LOGOUT_FAIL);
    }

    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询", notes = "根据id进行用户查询")
    public ResponseDTO<UserDTO> queryById(@PathVariable Long id) {
        return ResponseDTO.res(ConvertUtils.copy(userService.getById(id), UserDTO.class), ResponseCodeEnum.USER_NOTFOUND);
    }

    @PostMapping("/query/all")
    @ApiOperation(value = "查询全部")
    public ResponseDTO<PageResultDTO<UserDTO>> findAll(){
        return ResponseDTO.res(userService.findPage(new UserQueryDTO()), ResponseCodeEnum.USER_NOTFOUND);
    }

    @PostMapping(value="/query/page")
    @ApiOperation(value = "条件查询")
    public ResponseDTO<PageResultDTO<UserDTO>> findPage(UserQueryDTO userQueryDTO) {
        return ResponseDTO.res(userService.findPage(userQueryDTO), ResponseCodeEnum.USER_NOTFOUND);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增")
    public ResponseDTO<String> addUser(UserAddDTO addDTO) {
        if (addDTO.getLoginName() == null || addDTO.getLoginPwd() == null || addDTO.getActualName() == null)
            return ResponseDTO.err(ResponseCodeEnum.USER_INFO_NULL);
        if (userService.isUserNameExist(addDTO.getLoginName()))
            return ResponseDTO.err(ResponseCodeEnum.USER_NAME_EXIST);
        return ResponseDTO.res(userService.save(addDTO));
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新")
    public ResponseDTO<String> updateUser(UserUpdateDTO userUpdateDTO) {
        return ResponseDTO.res(userService.update(userUpdateDTO));
    }

    @PostMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除")
    public ResponseDTO<String> deleteUserById(@PathVariable Long id) {
        try {
            return ResponseDTO.res(userService.deleteById(id));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }

    @PostMapping("/delete/list")
    @ApiOperation(value = "批量删除")
    public ResponseDTO<String> deleteList(IDQueryDTO idQueryDTO){
        try {
            return ResponseDTO.res(userService.deleteList(idQueryDTO.getIdList()));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }
}
